
<template>
  <div>
    <ay-aside :page-info="pageInfo" />
    <div class="app-container">
      <div class="filter-container">

        <el-collapse v-model="filterNameKey" @change="handleChange">
          <el-collapse-item title="筛选条件" name="1">
            <el-row>

              <el-date-picker
                v-model="submitTimeTemp"
                class="filter-item"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-row>
          </el-collapse-item>
        </el-collapse>

        <p />

        <el-button v-show="isShowFilterBtn" class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">
          搜索
        </el-button>
        <el-button v-show="isShowFilterBtn" class="filter-item" type="danger" icon="el-icon-close" @click="handleReset">
          重置
        </el-button>

      </div>

      <div :id="id" :class="className" :style="{height:height,width:width}" />

    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'
import { statistics } from '@/api/c2cUserPage'
import waves from '@/directive/waves' // waves directive
export default {
  name: 'TypeList',
  directives: { waves },
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    }
  },
  data() {
    return {
      submitTimeTemp: '', // 提交时间中间量
      pageInfo: {},
      chart: null,
      allNum: [],
      addNum: [],
      xData: [],
      isShowFilterBtn: true,
      filterNameKey: '1',
      search: {
        startTime: '',
        submitTime: '', // 提交时间
        endTime: ''
      }

    }
  },

  watch: {
    submitTimeTemp(newval, oldval) {
      if (!newval) {
        this.search.submitTime = ''
      } else {
        const start = new Date(this.submitTimeTemp[0])
        const resStartDate = start.getFullYear() + '-' + this.doTwoDate((start.getMonth() + 1)) + '-' + this.doTwoDate(start.getDate())
        const resStartTime = this.doTwoDate(start.getHours()) + ':' + this.doTwoDate(start.getMinutes()) + ':' + this.doTwoDate(start.getSeconds())
        const end = new Date(this.submitTimeTemp[1])
        const resEndDate = end.getFullYear() + '-' + this.doTwoDate((end.getMonth() + 1)) + '-' + this.doTwoDate(end.getDate())
        const resEndTime = this.doTwoDate(end.getHours()) + ':' + this.doTwoDate(end.getMinutes()) + ':' + this.doTwoDate(end.getSeconds())
        this.search.startTime = `${resStartDate} ${resStartTime}`
        this.search.endTime = `${resEndDate} ${resEndTime}`
      }
    }
  },

  mounted() {
    this.getData()
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 处理日期
    doTwoDate(s) {
      return s < 10 ? '0' + s : s
    },

    getData() {
      console.log(this.search)

      const param = {}
      Object.assign(param, {

      }, this.search)
      statistics(param).then((response) => {
        for (const i in response.data) {
          this.allNum.push(response.data[i]['num'])
          this.addNum.push(response.data[i]['addNum'])
          this.xData.push(response.data[i]['date'])
        }
        this.initChart()
      })
    },
    handleChange(val) {
      if (val.length === 0) {
        this.isShowFilterBtn = false
      } else {
        this.isShowFilterBtn = true
      }
    },
    handleRefresh() {
    },
    handleReset() {
      this.search = {
        startTime: '',
        endTime: ''
      }
      this.submitTimeTemp = ''
    },
    handleSearch() {
      this.allNum = []
      this.addNum = []
      this.xData = []
      this.getData()
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        backgroundColor: '#344b58',
        title: {
          text: '卖家数分析',
          x: '20',
          top: '20',
          textStyle: {
            color: '#fff',
            fontSize: '22'
          },
          subtextStyle: {
            color: '#90979c',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 150,
          bottom: 95,
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          x: '5%',
          top: '10%',
          textStyle: {
            color: '#90979c'
          },
          data: ['female', 'male', 'average']
        },
        calculable: true,
        xAxis: [{
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitArea: {
            show: false
          },
          axisLabel: {
            interval: 0

          },
          data: this.xData
        }],
        yAxis: [{
          type: 'value',
          splitLine: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0
          },
          splitArea: {
            show: false
          }
        }],
        dataZoom: [{
          show: true,
          height: 30,
          xAxisIndex: [
            0
          ],
          bottom: 30,
          start: 10,
          end: 80,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: '#d3dee5'

          },
          textStyle: {
            color: '#fff' },
          borderColor: '#90979c'

        }, {
          type: 'inside',
          show: true,
          height: 15,
          start: 1,
          end: 35
        }],
        series: [{
          name: '新增卖家数',
          type: 'bar',
          stack: 'total',
          barMaxWidth: 35,
          barGap: '10%',
          itemStyle: {
            normal: {
              color: 'rgba(0,191,183,1)',
              label: {
                show: true,
                textStyle: {
                  color: '#fff'
                },
                position: 'insideTop',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: this.addNum
        }, {
          name: '前一天卖家数',
          type: 'line',
          stack: 'total',
          symbolSize: 10,
          symbol: 'circle',
          itemStyle: {
            normal: {
              color: 'rgba(252,230,48,1)',
              barBorderRadius: 0,
              label: {
                show: true,
                position: 'top',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: this.allNum
        }
        ]
      })
    }
  }
}
</script>

